<?php
/**
 * 熊猫试玩 - 创建下载记录
 * Created by PhpStorm.
 * User: lizhen
 * Date: 2016/9/5
 * Time: 15:11
 */

class PandaPlayCreateDown implements iController {

    /**
     * 点击请求 - 创建下载记录
     * @param $queryArgs idfa:设备ID;appId:APPID;callback:回调地址;
     * @return array|mixed
     */
    public function output($queryArgs) {

        global $uddb;

        if (!empty($queryArgs['idfa']) && !empty($queryArgs['appId']) && !empty($queryArgs['callback']) && !empty($queryArgs['sign'])){

            //验证签名
            if (md5($queryArgs['idfa'].$queryArgs['appId'].'yaoguo') != $queryArgs['sign'])
                return json_encode(['status'=> 0]);//签名不匹配

            //检测记录是否存在
            $exists = $uddb->getRow('SELECT id,callback FROM `youqian_idfa_data` WHERE `app_id`=:appId  AND  `idfa`=:idfa AND `channel_id`= \'pandaPlay\' ',
                [':idfa' => $queryArgs['idfa'] ,':appId' => $queryArgs['appId'] ]);

            if(!$exists){
                //创建下载记录
                $result = $uddb->insert("youqian_idfa_data", array(
                    'app_id' => $queryArgs['appId'],
                    'idfa' => $queryArgs['idfa'],
                    'channel_id' => 'pandaPlay',
                    'mission_status' => '0',
                    'callback' => $queryArgs['callback'],
                    'advertiser_id' => '0',
                    'click_id' => '0',
                    'click_time' => date('Y-m-d H:i:s')
                ));
                if($result)
                    return json_encode(['status'=> 1]);
                else
                    return json_encode(['status'=> 0]);//记录创建失败
            }else
                return json_encode(['status'=> 0]);//记录已存在
        }
        return json_encode(['status'=> 0]);//缺少参数
    }

}